package cn.iocoder.yudao.module.project.service.weighthistoryorderedit;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.weighthistoryorderedit.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.weighthistoryorderedit.WeightHistoryOrderEditDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.weighthistoryorderedit.WeightHistoryOrderEditMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 订单编辑-删除相关称重业务 Service 实现类
 *
 * @author wangzuoyang
 */
@Service
@Validated
public class WeightHistoryOrderEditServiceImpl implements WeightHistoryOrderEditService {

    @Resource
    private WeightHistoryOrderEditMapper weightHistoryOrderEditMapper;

    @Override
    public Long createWeightHistoryOrderEdit(WeightHistoryOrderEditSaveReqVO createReqVO) {
        // 插入
        WeightHistoryOrderEditDO weightHistoryOrderEdit = BeanUtils.toBean(createReqVO, WeightHistoryOrderEditDO.class);
        weightHistoryOrderEditMapper.insert(weightHistoryOrderEdit);
        // 返回
        return weightHistoryOrderEdit.getRecycleId();
    }

    @Override
    public void updateWeightHistoryOrderEdit(WeightHistoryOrderEditSaveReqVO updateReqVO) {
        // 校验存在
        validateWeightHistoryOrderEditExists(updateReqVO.getRecycleId());
        // 更新
        WeightHistoryOrderEditDO updateObj = BeanUtils.toBean(updateReqVO, WeightHistoryOrderEditDO.class);
        weightHistoryOrderEditMapper.updateById(updateObj);
    }

    @Override
    public void deleteWeightHistoryOrderEdit(Long id) {
        // 校验存在
        validateWeightHistoryOrderEditExists(id);
        // 删除
        weightHistoryOrderEditMapper.deleteById(id);
    }

    private void validateWeightHistoryOrderEditExists(Long id) {
        if (weightHistoryOrderEditMapper.selectById(id) == null) {
            throw exception(WEIGHT_HISTORY_ORDER_EDIT_NOT_EXISTS);
        }
    }

    @Override
    public WeightHistoryOrderEditDO getWeightHistoryOrderEdit(Long id) {
        return weightHistoryOrderEditMapper.selectById(id);
    }

    @Override
    public PageResult<WeightHistoryOrderEditDO> getWeightHistoryOrderEditPage(WeightHistoryOrderEditPageReqVO pageReqVO) {
        return weightHistoryOrderEditMapper.selectPage(pageReqVO);
    }

}